package com.example.scribe.backend;

import java.util.Iterator;

/**
 * Helper class for range operations.
 * Provides methods to create Iterable<Integer> for range expressions.
 */
public final class RangeHelper {
    private RangeHelper() {}

    /**
     * Creates an exclusive range: start..end (end is exclusive)
     * Example: range(0, 10) returns [0, 1, 2, ..., 9]
     */
    public static Iterable<Integer> range(int start, int end) {
        return new IntRange(start, end, false);
    }

    /**
     * Creates an inclusive range: start..=end (end is inclusive)
     * Example: rangeInclusive(0, 10) returns [0, 1, 2, ..., 10]
     */
    public static Iterable<Integer> rangeInclusive(int start, int end) {
        return new IntRange(start, end, true);
    }

    /**
     * Simple integer range implementation.
     */
    private static final class IntRange implements Iterable<Integer> {
        private final int start;
        private final int end;
        private final boolean inclusive;

        IntRange(int start, int end, boolean inclusive) {
            this.start = start;
            this.end = end;
            this.inclusive = inclusive;
        }

        @Override
        public Iterator<Integer> iterator() {
            return new IntRangeIterator(start, end, inclusive);
        }
    }

    /**
     * Iterator for integer ranges.
     */
    private static final class IntRangeIterator implements Iterator<Integer> {
        private final int end;
        private final boolean inclusive;
        private int current;

        IntRangeIterator(int start, int end, boolean inclusive) {
            this.current = start;
            this.end = end;
            this.inclusive = inclusive;
        }

        @Override
        public boolean hasNext() {
            if (inclusive) {
                return current <= end;
            } else {
                return current < end;
            }
        }

        @Override
        public Integer next() {
            if (!hasNext()) {
                throw new java.util.NoSuchElementException();
            }
            return current++;
        }
    }
}

